﻿using System.Web.Mvc;
using doufu.Common;
using doufu.Model;

namespace doufu.WebUI.Controllers
{
    [UserAuthorize]
    public class CategoryController : BaseController
    {
        public ActionResult Index()
        {
            Title = "类型列表";
            return View(Services.Category.List());
        }

        public ActionResult Add(int? id)
        {
            var entity = id.HasValue ? Services.Category.Details(id.Value) : new category();
            Title = entity.category_id == 0 ? "添加类型" : string.Format("修改类型:{0}", entity.category_name);
            return View(entity);
        }

        [HttpPost]
        public ActionResult Add(category entity)
        {
            if (ModelState.IsValid)
            {
                var ret = Services.Category.SaveCategory(entity, this);
                if (ret)
                {
                    return RedirectToAction("Index");
                }
                ViewBag.RetMsg = "已存在相同的类！";
                return View();
            }
            return View(entity);
        }

    }
}
